package com.example.jartest;

import android.graphics.Color;
import android.os.Bundle;
import android.speech.tts.TextToSpeechService;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private List<String> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();

    private TextView time;
    private TextView options;

    private TimePickerView tv_Time;
    private OptionsPickerView pv_Options;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        options1Items.add("动漫");
        options1Items.add("游戏");
        ArrayList<String> stringList1 = new ArrayList<String>();
        stringList1.add("伊藤诚");
        stringList1.add("五河士道");
        ArrayList<String> stringList2 = new ArrayList<String>();
        stringList2.add("公孙离");
        stringList2.add("安琪拉");
        options2Items.add(stringList1);
        options2Items.add(stringList2);
        ArrayList<ArrayList<String>> list1 = new ArrayList<>();
        ArrayList<String> string = new ArrayList<String>();
        string.add("桂言叶");
        string.add("西园寺世界");
        string.add("清浦刹那");
        string.add("加藤乙女");
        string.add("黑田光");
        list1.add(string);
        string = new ArrayList<String>();
        string.add("夜刀神十香");
        string.add("鸢一折纸");
        string.add("五河琴里");
        string.add("四糸乃");
        string.add("时崎狂三");
        string.add("八舞夕弦");
        list1.add(string);
        ArrayList<ArrayList<String>> list2 = new ArrayList<>();
        string = new ArrayList<String>();
        string.add("蜜橘之夏");
        string.add("无限欣赏官");
        string.add("花间舞");
        list2.add(string);
        string = new ArrayList<String>();
        string.add("玩偶对对碰");
        string.add("魔法小厨娘");
        string.add("心灵骇客");
        string.add("如懿");
        list2.add(string);
        options3Items.add(list1);
        options3Items.add(list2);
        initOptionsPicker();
        initTimePicker();
        initView();
    }
    private void initOptionsPicker(){
        pv_Options = new OptionsPickerBuilder(MainActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String tx = options1Items.get(options1)
                        + options2Items.get(options1).get(options2)
                        + options3Items.get(options1).get(options2).get(options3);
                    options.setText(tx);
            }
        })
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setTitleText("选择")//标题
                .setSubCalSize(18)//确定和取消文字大小
                .setTitleSize(20)//标题文字大小
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTitleBgColor(0xFF333333)//标题背景颜色 Night mode
                .setBgColor(0xFFffffff)//滚轮背景颜色 Night mode
                .setContentTextSize(18)//滚轮文字大小
                .setLabels("", "", "")//设置选择的三级单位
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setCyclic(false, false, false)//循环与否
                .setSelectOptions(1, 1, 1)  //设置默认选中项
                .setOutSideCancelable(false)//点击外部dismiss default true
                .isDialog(true)//是否显示为对话框样式
                .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                .build();
        pv_Options.setPicker(options1Items,options2Items,options3Items);
    }
    private void initTimePicker() {
        tv_Time = new TimePickerBuilder(MainActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {

            }
        }).build();
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        SimpleDateFormat formatter_year = new SimpleDateFormat("yyyy ");
        String year_str = formatter_year.format(curDate);
        int year_int = (int) Double.parseDouble(year_str);


        SimpleDateFormat formatter_mouth = new SimpleDateFormat("MM ");
        String mouth_str = formatter_mouth.format(curDate);
        int mouth_int = (int) Double.parseDouble(mouth_str);

        SimpleDateFormat formatter_day = new SimpleDateFormat("dd ");
        String day_str = formatter_day.format(curDate);
        int day_int = (int) Double.parseDouble(day_str);

        Calendar Date = Calendar.getInstance();
        Date.set(year_int, mouth_int, day_int);
        Calendar startDate = Calendar.getInstance();
        startDate.set(1900, 0, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(year_int, mouth_int + 1, day_int);

        tv_Time = new TimePickerBuilder(MainActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
                time.setText(sdr.format(date));
            }
        })
                .setType(new boolean[]{true, true, true, true, true, true})//默认全部显示
                .setCancelText("Cancel")//取消按钮文字
                .setSubmitText("Sure")//确认按钮文字
                .setContentTextSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setTitleText("Title")//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTitleBgColor(0xFFffffff)//标题背景颜色 Night mode
                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
                .setRangDate(startDate, endDate)//默认是1900-2100年
                .setDate(Date)// 默认是系统时间*/
                .setLabel("年","月","日","时","分","秒")
                .build();
    }

    private void initView() {
        time = (TextView) findViewById(R.id.time);
        time.setOnClickListener(this);
        options = (TextView) findViewById(R.id.options);
        options.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.time:
                tv_Time.show(view);
                break;
            case R.id.options:
                pv_Options.show(view);
                break;
        }
    }
}
